package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.EmpMapper;
import com.sky.result.PageResult;
import com.sky.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    @Override
    public void upDate(PasswordEditDTO passwordEditDTO) {
        String newPassword = passwordEditDTO.getNewPassword();
        String oldPassword = passwordEditDTO.getOldPassword();
        newPassword = DigestUtils.md5DigestAsHex(newPassword.getBytes());
        oldPassword = DigestUtils.md5DigestAsHex(oldPassword.getBytes());
        Employee employee = empMapper.findById(passwordEditDTO.getEmpId());


        if (oldPassword.equals(employee.getPassword())) {

            employee.setUpdateTime(LocalDateTime.now());
            employee.setPassword(newPassword);

            empMapper.upDate(employee);
        } else {
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

    }

    @Override
    public void status(Integer status, Long id) {
        empMapper.status(status, id);

    }

    @Override
    public PageResult findAll(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page, pageSize);
        List<Employee> list = empMapper.findAll(name);
        Page<Employee> p = (Page<Employee>) list;
        PageResult pageResult = new PageResult(p.getTotal(), p.getResult());
        return pageResult;
    }

    @Override
    public void add(EmployeeDTO employeeDTO) {
        Employee employee = Employee.builder()
                .id(employeeDTO.getId())
                .name(employeeDTO.getName())
                .username(employeeDTO.getUsername())
                .password(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()))
                .phone(employeeDTO.getPhone())
                .sex(employeeDTO.getSex())
                .idNumber(employeeDTO.getIdNumber())
                .status(StatusConstant.ENABLE)
                //.createTimec(LocalDateTime.now())
                //.updateTime(LocalDateTime.now())
                       // .createUser(BaseContext.getCurrentId())
                .build();

        empMapper.add(employee);

    }

    @Override
    public Employee findById(Long id) {
        Employee employee = empMapper.findById(id);
        return employee;
    }

    @Override
    public void upDateEmp(EmployeeDTO employeeDTO) {
        Employee empMapperById = empMapper.findById(employeeDTO.getId());
        empMapperById.setUpdateTime(LocalDateTime.now());
        empMapperById.setCreateUser(BaseContext.getCurrentId());
        empMapperById.setName(employeeDTO.getName());
        empMapperById.setUsername(employeeDTO.getUsername());
        empMapperById.setPhone(employeeDTO.getPhone());
        empMapperById.setSex(employeeDTO.getSex());
        empMapperById.setIdNumber(employeeDTO.getIdNumber());
        empMapper.upDateEmp(empMapperById);
    }
}